<template>
  <div class="app-container new-style">
    <el-button style="position: absolute;left:20px;top:20px" type="primary" @click="goBack">返回</el-button>
    <div style="margin-bottom: 10px;">
      <h2>新增固定资产月度统计表列表</h2>
    </div>
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item label="">
          <el-date-picker v-model="listQuery.monthDate" type="month" placeholder="选择月份进行筛选" value-format="yyyy-MM" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">查询</el-button>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 + (listQuery.page - 1) * listQuery.limit }}
        </template>
      </el-table-column>
      <el-table-column label="月份">
        <template slot-scope="scope">
          {{ scope.row.monthDate }}
        </template>
      </el-table-column>
      <el-table-column label="统计日期">
        <template slot-scope="scope">
          {{ scope.row.statisticsYear }}-{{ scope.row.statisticsMonth }}-{{ scope.row.statisticsDay }}
        </template>
      </el-table-column>
      <el-table-column label="复核人">
        <template slot-scope="scope">
          {{ scope.row.reviewer }}
        </template>
      </el-table-column>
      <el-table-column label="审核人">
        <template slot-scope="scope">
          {{ scope.row.approver }}
        </template>
      </el-table-column>
      <el-table-column label="制表人">
        <template slot-scope="scope">
          {{ scope.row.creator }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleView(scope.row)">查看</el-button>
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
  </div>
</template>

<script>
import { getStatisticsList, deleteStatistics } from '@/api/fixedAssetsStatistics'
import Pagination from '@/components/Pagination'

export default {
  name: 'FixedAssetsStatisticsList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        monthDate: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const response = await getStatisticsList({
          current: this.listQuery.page,
          size: this.listQuery.limit,
          monthDate: this.listQuery.monthDate
        })
        this.list = response.data.records
        this.total = response.data.total
      } catch (error) {
        this.$message.error('获取列表失败：' + error.message)
      }
      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    goBack() {
      this.$router.push('/main/xingzheng/gdzctongji/gdzctongji')
    },
    handleAdd() {
      this.$router.push('/main/xingzheng/gdzctongji/gdzctongji')
    },
    handleView(row) {
      this.$router.push({
        path: '/main/xingzheng/gdzctongji/gdzctongji',
        query: {
          id: row.id,
          type: 'view'
        }
      })
    },
    handleEdit(row) {
      this.$router.push({
        path: '/main/xingzheng/gdzctongji/gdzctongji',
        query: {
          id: row.id,
          type: 'edit'
        }
      })
    },
    async handleDelete(row) {
      try {
        await this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteStatistics(row.id)
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 新增样式类
.new-style {
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  min-height: calc(100vh - 40px);
}

.filter-container {

  .filter-item {
    display: inline-block;
    vertical-align: middle;
  }
}

// 优化表格样式
.el-table {
  border-radius: 8px;
  overflow: hidden;

  th {
    background-color: #f2f2f2;
    color: #333;
  }

  tr:hover {
    background-color: #f5f5f5;
  }
}

// 优化分页样式
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>